package leetcode_core.leetcode_2;

public class Partition {
    public class ListNode {
          int val;
          ListNode next;
          ListNode() {}
          ListNode(int val) { this.val = val; }
          ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode partition(ListNode head, int x) {
        ListNode dummy1 = new ListNode(-1);//放较小的值
        ListNode dummy2 = new ListNode(-1);//放较大的值
        ListNode p1 = dummy1,p2=dummy2;
        ListNode p = head;
        while (p!=null){
            if(p.val <= x){
                p1.next = p;
                p1 = p1.next;
            }else{
                p2.next = p;
                p2 = p2.next;
            }
            //注意这里还要断开每个节点的next指针
            ListNode temp = p.next;
            p.next = null;
            p = temp;
        }
        p1.next = dummy2.next;
        return dummy1.next;
    }


}
